﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
using System.Xml.Xsl;

namespace ToolKitIESiteList.Library
{
    public static class ManagerHotTracker
    {
        private static List<HotTracker> siteTracker = new List<HotTracker>();
        private static bool trackEnabled = true;

        public static void TrackerEnable(bool enabled)
        {
            trackEnabled = enabled;
        }
        public static ReadOnlyCollection<HotTracker> DataTracker()
        {
            return new ReadOnlyCollection<HotTracker>(siteTracker);
        }

        public static void SaveTofile(string fileName)
        {
            var siteEntries = siteTracker.OrderBy(s =>s.DtRef) .Select(entry => new XElement("Traker",
                                new XElement("DtRef",string.Format("{0} {1}",entry.DtRef.ToShortDateString(), entry.DtRef.ToShortTimeString())),
                                new XElement("Action", entry.Action),
                                new XElement("Description", entry.ActionDescription),
                                new XElement("Complement", entry.ActionComplement),
                                new XElement("Sites", entry.Sites.Select(s =>
                                    {
                                        var ele = new XElement("Site", s.HostPort + s.Path);
                                        ele.SetAttributeValue("CompatMode", s.CompatMode);
                                        ele.SetAttributeValue("OpenIn", s.OpenIn);
                                        return ele;
                                    }).ToArray())));
            XElement siteList = new XElement("Trakers", siteEntries);
            //Salva o arquivo em disco
            siteList.Save(fileName);
        }

        public static void ConverToHtml(string hotfile,string filename,string version, string date)
        {
            Assembly a = Assembly.GetExecutingAssembly();
            Stream stream = a.GetManifestResourceStream("ToolKitIESiteList.Library.Schema.HotTrackerConvertHtml.xslt");
            XmlReader stylesheet = XmlReader.Create(stream, new XmlReaderSettings()
            {
                CloseInput = true,
                IgnoreComments = true
            });

            XsltArgumentList xslArgs = new XsltArgumentList();
            xslArgs.AddParam("FileName", "", filename);
            xslArgs.AddParam("VersionSiteList", "", version);
            xslArgs.AddParam("DateSiteList", "", date);

            var  xslt = new XslCompiledTransform();
            xslt.Load(stylesheet);

            XPathDocument xpath = new XPathDocument(hotfile);
            if (File.Exists(filename))
            {
                File.Delete(filename);
            }
            using (var xwriter = new XmlTextWriter(filename, Encoding.UTF8))
            {
                xslt.Transform(xpath, xslArgs, xwriter, null);
            }
        }

        public static void Clear()
        {
            siteTracker = new List<HotTracker>();
        }

        public static void AddTraker(ActionTracker action, string description, string complement, IEnumerable<IEModeDataItem> sites)
        {
            if (!trackEnabled) return;

            if (sites == null)
            {
                sites = new IEModeDataItem[] { };
            }

            siteTracker.Add(new HotTracker
            {
                Action = action,
                ActionDescription = description,
                ActionComplement = complement,
                DtRef = DateTime.Now,
                Sites = sites.Select(s => new DataTraker
                {
                    HostPort = s.Uri.GetHostAndPort(),
                    CompatMode = s.CompatMode,
                    OpenIn = s.OpenIn,
                    Path =  s.Uri.AbsolutePath == "/"?"":s.Uri.AbsolutePath
                }).ToArray()
            });
        }
    }
}
